package tech.jabari.user.controller;

import tech.jabari.utils.FileUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 获取服务器下载目录中的文件列表。
 * @author Jabari Lu
 * @title: UploadServlet
 * @data 2020/8/14 13:34
 */
@WebServlet("/listFiles.action")
public class ListFilesServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String fileRoot = this.getServletContext().getRealPath("/upload");
        File root = new File(fileRoot);
        if (!root.exists()) {
            return;
        }

        Map<String, String> fileMap = new HashMap<>();
        FileUtils.putFiles(root, fileMap);
        req.setAttribute("files", fileMap);
        req.getRequestDispatcher("listFiles.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    }
}
